package copyengine.utils.delayEffect.effect.basic.old
{
	import copyengine.utils.debug.CELog;

	import flash.display.DisplayObjectContainer;
	import copyengine.utils.delayEffect.effect.ICEDelayEffect;

	/**
	 * <b>当前Effect特性为:</b></br>
	 * <b>包含子Child的Delayeffect,且子Child是顺序执行</b>
	 * </br></br>===========================</br></br>
	 * <b>所有Effect特性为:</b>
	 * <li>BasicEffect 					不包含子Child,可以Override,当Execute时候->主逻辑执行完成及为完成
	 * <li>MacroEffect 					包含子Child,不可以Override,当ExecuteBranch时候->所有子Child一同执行
	 * <li>ContianBranchEffect		包含子Child,可以Override,当ExecuteBranch时候->让所有Branch按顺序执行
	 * <li>SelectableChildEffect	包含子Child,可以Override,当ExecuteBranch时候->选择应该执行哪个Branch
	 */
	public class CEDelayContainBranchEffectOld implements ICEDelayEffect
	{
		protected var sceneUILayer:DisplayObjectContainer;
		protected var sceneLayer:DisplayObjectContainer;
		protected var globalUILayer:DisplayObjectContainer;

		private var trunkFlowFinishCallBack:Function;
		private var trunkExecuteFinishCallBack:Function;

		private var isTrunkExecuteFinish:Boolean = false;
		private var isBranchExecuteFinish:Boolean = false;

		private var isExecuteDataHandleLogic:Boolean = false;

		private var branchExecuteFinishNum:int = 0;
		private var currentBranchFlowExecuteStep:int = -1;

		/**
		 * 枝干EffectVector，枝干上面的Effect按照add的顺序，一个接着一个播放
		 */
		private var subBranchEffectVector:Vector.<ICEDelayEffect>;

		public function CEDelayContainBranchEffectOld()
		{
			subBranchEffectVector = new Vector.<ICEDelayEffect>();
		}

		public final function addSubBranchEffect(_effect:ICEDelayEffect):void
		{
			subBranchEffectVector.push(_effect);
		}

		public final function setLayer(_sceneUILayer:DisplayObjectContainer, _sceneLayer:DisplayObjectContainer, _globalUILayer:DisplayObjectContainer):void
		{
			sceneUILayer = _sceneUILayer;
			sceneLayer = _sceneLayer;
			globalUILayer = _globalUILayer;
			for each (var branchEffect:ICEDelayEffect in subBranchEffectVector)
			{
				branchEffect.setLayer(sceneUILayer, sceneLayer, globalUILayer);
			}
		}

		public final function execute(_flowFinishCallBack:Function, _executeFinishCallBack:Function):void
		{
			trunkFlowFinishCallBack = _flowFinishCallBack;
			trunkExecuteFinishCallBack = _executeFinishCallBack;
			doExecuteTrunk();
			if (subBranchEffectVector.length <= 0)
			{
				isBranchExecuteFinish = true;
			}
		}

		public final function dispose():void
		{
			trunkExecuteFinishCallBack = null;
			trunkFlowFinishCallBack = null;

			doDispose();
			while (subBranchEffectVector.length > 0)
			{
				subBranchEffectVector.pop().dispose();
			}

			subBranchEffectVector = null;
		}

		public final function isExecuteFinish():Boolean
		{
			return isTrunkExecuteFinish && isBranchExecuteFinish;
		}

		public final function finishExecuteImmediately():void
		{
			for each (var branchEffect:ICEDelayEffect in subBranchEffectVector)
			{
				if (!branchEffect.isExecuteFinish())
				{
					branchEffect.finishExecuteImmediately();
				}
			}
			tryToExecuteDataHandleLogic();
		}

		/**
		 * <b>执行当前Trunk上面的所有剩余的Branch分支Effec</b>
		 */
		protected final function executeAllRemainBranch():void
		{
			currentBranchFlowExecuteStep = -1;
			if (subBranchEffectVector.length > 0)
			{
				tryToExecuteNextBranchFlow();
			}
			else
			{
				//没有branchEffect
				isBranchExecuteFinish = true;
				tryToFinishExecute();
			}
		}

		/**
		 *主干上面执行完成 尝试结束当前Effect
		 */
		protected final function onTrunkExecuteFinish():void
		{
			isTrunkExecuteFinish = true;
			tryToFinishExecute();
			tryToExecuteDataHandleLogic();
		}

		/**
		 *当主干上面的流程执行结束后调用该函数
		 */
		protected final function onTrunkFlowFinish():void
		{
			trunkFlowFinishCallBack && trunkFlowFinishCallBack();
			trunkFlowFinishCallBack = null;
		}

		public final function onFlowExecuteFinish():void
		{
			doOnTrunkExecuteFinish();
			for each (var effect:ICEDelayEffect in subBranchEffectVector)
			{
				effect.onFlowExecuteFinish();
			}
		}

		protected final function getLayerByType(_type:int):DisplayObjectContainer
		{
			if (_type == CEDelayEffectBasicOld.LAYER_TYPE_SCENE)
			{
				return sceneLayer;
			}
			else if (_type == CEDelayEffectBasicOld.LAYER_TYPE_SCENE_UI)
			{
				return sceneUILayer;
			}
			else if (_type == CEDelayEffectBasicOld.LAYER_TYPE_GLOBAL_UI)
			{
				return globalUILayer;
			}
			else
			{
				CELog.err("CEDelayEffectPro-> can not find layer by type : " + _type);
				return null;
			}
		}

		//==================
		//==OverrideAble Function
		//==================
		protected function doDispose():void
		{
		}

		protected function doExecuteTrunk():void
		{
		}

		protected function executeDataHandleLogic():void
		{
		}

		protected function doOnTrunkExecuteFinish():void
		{
		}

		//===============
		//==Private
		//===============
		private function tryToExecuteNextBranchFlow():void
		{
			currentBranchFlowExecuteStep++;
			if (currentBranchFlowExecuteStep < subBranchEffectVector.length)
			{
				var effect:ICEDelayEffect = subBranchEffectVector[currentBranchFlowExecuteStep];
				effect.execute(tryToExecuteNextBranchFlow, onBranchExecuteFinish);
			}
		}

		private function onBranchExecuteFinish():void
		{
			branchExecuteFinishNum++;
			if (branchExecuteFinishNum >= subBranchEffectVector.length)
			{
				isBranchExecuteFinish = true;
				tryToFinishExecute();
			}
		}

		private function tryToFinishExecute():void
		{
			if (isTrunkExecuteFinish && isBranchExecuteFinish)
			{
				trunkExecuteFinishCallBack && trunkExecuteFinishCallBack();
			}
		}

		private function tryToExecuteDataHandleLogic():void
		{
			if (!isExecuteDataHandleLogic)
			{
				executeDataHandleLogic();
			}
			isExecuteDataHandleLogic = true;
		}

	}
}
